//
//  MK_HomePageViewController.m
//  miku直播
//
//  Created by 杨尚达 on 2017/7/23.
//  Copyright © 2017年 杨尚达. All rights reserved.
//

#import "MK_HomePageViewController.h"
#import "MK_TitleContentVC.h"
#import "MK_Content_Cell.h"
#import "MK_HomePageViewModel.h"
#import "MK_HorizonShowView.h"
#import "MK_LivingRoom_ImageView.h"
#import "MK_LivingRoomVC.h"

///cell的重用标识
static NSString* MK_Content_Cell_Name = @"MK_Content_Cell_Name";

@interface MK_HomePageViewController () <UICollectionViewDelegate,UICollectionViewDataSource>

///ViewModel
VIEWMODEL_PROPERTY(MK_HomePageViewModel)

///状态按钮
@property(nonatomic,weak)UIButton* stateBu;

///首页分类数组
@property(nonatomic,strong)NSArray<MK_HomeCategoryModel*>* categoryArr;

///推荐模型
@property(nonatomic,strong)NSArray<MK_Content_Model*>* recommendModelArr;

///内容视图
@property(nonatomic,strong)MK_TitleContentVC* contentV;

///轮播器模型
@property(nonatomic,strong)NSArray<MK_HomeSlideItem*>* slidArr;

@end

@implementation MK_HomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpUI];
    [self setRAC];
    [self loadData];
}

///初始化界面
-(void)setUpUI{
    UIButton* stateBu = [UIButton new];
    _stateBu = stateBu;
    stateBu.titleLabel.textAlignment = NSTextAlignmentCenter;
    stateBu.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:stateBu];
    [stateBu setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    stateBu.userInteractionEnabled = NO;
    [stateBu mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    [[stateBu rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id  _Nullable x) {
        [_contentV reloadContentData];
    }];
    
    ///跳转直播间的通知
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:Notification_ClickLivingRoomModel object:nil] subscribeNext:^(NSNotification*  _Nullable x) {
        MK_LivingRoomVC* roomVC = [MK_LivingRoomVC MK_LivingRoomWithRoomModel:(MK_LivingRoomModel*)x.object];
        [self presentViewController:roomVC animated:YES completion:nil];
    }];
    
    
}


///配置rac方法
-(void)setRAC{
    [RACObserve(self.vM, state) subscribeNext:^(id x) {
        switch ([x intValue]) {
            case MK_HomePageViewModel_State_Loading:
                self.stateBu.userInteractionEnabled = NO;
                [self.stateBu setTitle:@"正在加载中" forState:UIControlStateNormal];
                break;
            case MK_HomePageViewModel_State_Success:
                self.stateBu.hidden = YES;
                break;
            case MK_HomePageViewModel_State_FailFailed:
                self.stateBu.userInteractionEnabled = YES;
                [self.stateBu setTitle:@"加载失败" forState:UIControlStateNormal];
                break;
        }
    }];
}


#pragma mark 内容视图代理方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.categoryArr.count+1;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MK_Content_Cell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:MK_Content_Cell_Name forIndexPath:indexPath];
    cell.model = _recommendModelArr[indexPath.row];
    return cell;
}

#pragma mark 数据加载方法
///加载标题数据
-(void)loadData{
    [[self.vM.CategoryCommand execute:nil] subscribeNext:^(NSArray<MK_HomeCategoryModel *> *  _Nullable x) {
        self.categoryArr = x;
        [self setupContentView];
    }];
    
}

#pragma mark 加载内容界面
///加载内容界面
-(void)setupContentView{
    
    NSMutableArray<NSString*>* titleArr = @[@"推荐"].mutableCopy;
    NSMutableArray<NSString*>* IDArr = [NSMutableArray new];
    [self.categoryArr enumerateObjectsUsingBlock:^(MK_HomeCategoryModel*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [titleArr addObject:obj.title];
        [IDArr addObject:obj.identification];
    }];
    
    ///添加 标题-内容 视图
    MK_TitleContentVC* contVC = [MK_TitleContentVC MK_TitleContentVCWithFrame:self.view.bounds andTitleArr:titleArr andDelegate:self andTitleWidth:self.view.width/titleArr.count];
    
    contVC.cellNibDic = @{MK_Content_Cell_Name:[UINib nibWithNibName:@"MK_Content_Cell" bundle:nil]};
    _contentV = contVC;
    [self addChildViewController:contVC];
    [self.view insertSubview:contVC.view atIndex:0];
    
    ///加载首页数据
    [[self.vM.HotRoomCommand execute:nil] subscribeNext:^(NSArray<MK_HomeSlideItem*>*  _Nullable x) {
        _slidArr = x;
        [[[self.vM CategoryHotRoomCommandWithIDArr:IDArr] execute:nil] subscribeNext:^(RACTuple*  _Nullable x) {
            
            NSMutableArray<MK_Content_Model*>* contentArr = [NSMutableArray new];
            
            [titleArr enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                MK_Content_Model* model = [MK_Content_Model new];
                model.type = MK_Content_Model_Type_Other;
                model.livingRoomArr = x[idx];
                [contentArr addObject:model];
            }];
            contentArr.firstObject.type = MK_Content_Model_Type_Recommend;
            contentArr.firstObject.slideModelArr = _slidArr;
            _recommendModelArr = contentArr;
            [_contentV reloadContentData];
            self.vM.state = MK_HomePageViewModel_State_Success;
        }];
        
    }];
}

///get方法ViewModel
VIEWMODEL_GET(MK_HomePageViewModel)
@end
